<template>
    <div class="My_info" v-if="flag">
      <div class="My_myIcon">
        <img class="My_avatar" src="http://liufusong.top:8080/img/profile/avatar.png" alt="icon">
      </div>
      <div class="My_user">
        <div class="My_name">游客</div>
        <van-button type="primary" size="small" @click="goLogin"> 去登录 </van-button>
      </div>
    </div>
    <div class="My_info_user" v-else>
      <div class="My_myIcon">
        <img class="My_avatar" src="http://liufusong.top:8080/img/profile/avatar.png" alt="icon">
      </div>
      <div class="My_user">
        <div class="My_name_user">{{ username }}</div>
        <van-button
          round type="info"
          size="mini"
          style="width: 54px"
          @click="exitBtn"
        >退出</van-button>
        <div class="My_edit">
          编辑个人资料 <van-icon name="play" />
        </div>
      </div>
    </div>
</template>

<script>
import { Dialog } from 'vant'
import { removeToken } from '@/utils/storage'

export default {
  name: 'MyInfo',
  props: {
    token: {
      type: [String, Object],
      required: true
    },
    username: {
      type: String,
      required: true
    }
  },

  data () {
    return {
      flag: true
    }
  },
  created () {
    if (this.token) this.flag = false
  },
  methods: {
    goLogin () {
      this.$router.push('/login')
    },
    exitBtn () {
      Dialog.confirm({
        title: '提示',
        message: '是否确定退出?',
        width: '270px',
        confirmButtonColor: '#108ee9'
      })
        .then(() => {
          // on confirm
          this.$router.push('/login')
          removeToken()
        })
        .catch(() => {
          // on cancel
        })
    }
  }
}
</script>

<style scoped>
.My_info {
  position: absolute;
  background: #fff;
  width: 74%;
  height: 84%;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%)translateY(64%);
  box-shadow: 0 0 10px 3px #ddd;
  margin: 50px auto 0;
  padding: 0 20px;
  text-align: center;
  font-size: 13px;
}
.My_avatar {
  width: 100%;
  border-radius: 50%;
}
.My_myIcon {
  position: relative;
  transform: translateY(-50%);
  border-radius: 50%;
  width: 60px;
  height: 60px;
  border: 5px solid #f5f5f5;
  display: inline-block;
  box-shadow: 0 2px 2px #bdbdbd;
}
.My_name {
  margin-top: -17px;
  margin-bottom: 17px;
}
.My_name_user {
  color: #333;
  margin-top: -17px;
  margin-bottom: 8px;
}
.My_info_user {
  position: absolute;
  background: #fff;
  width: 74%;
  height: 56%;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  box-shadow: 0 0 10px 3px #ddd;
  margin: 50px auto 0;
  padding: 0 20px;
  text-align: center;
  font-size: 13px;
}
.My_edit {
  margin-top: 25px;
  color: #999;
  font-size: 12px;
}
</style>
